<?php

use Illuminate\Support\Facades\Redirect;
class HomeController extends BaseController {

	public function generate() {
		$user_ip = Request::getClientIp();

		//$user_ip = "49.147.160.178"; //comment on final
		$url = "http://ipinfo.io/$user_ip/json";
		$json = file_get_contents( $url );
		$obj = json_decode( $json );

		$faker = Faker\Factory::create();
		$code = $faker->unique()->bothify('??##?##???');
		$loc = $obj->loc;
		//$loc = str_replace(","," ",$loc);

		$search = new Search;
		$search->ip = $user_ip;
		$search->city = $obj->city;
		$search->location = $loc;
		$search->country = $obj->country;
		$search->accesscode = $code;

		$search->save();

		return View::make('pages.generate')->with('code',$code);
	}

	public function doSearch(){
		$se = Input::get('se');

		$rules = array( 'se' => 'min:3' );
		$validator = Validator::make(Input::all(), $rules);

		if ($validator->fails()){
			return Redirect::back()
				->withErrors($validator);
		}

		$date = date("Y-m-d H:i:s");
		$items = Item::where('item', 'LIKE', '%' . $se . '%')
			->whereRaw('DATE_ADD(created_at, INTERVAL 24 HOUR) > NOW()')
			->paginate(10);

		//var $user_ip = $_SERVER['REMOTE_ADDR'];
		$user_ip = Request::getClientIp();
		
		$url = "http://ipinfo.io/$user_ip/json";
		$json = file_get_contents( $url );
		$obj = json_decode( $json );

		$search = new Search;
		$search->desc = $se;
		$search->ip = $user_ip;
		$search->city = $obj->city;
		$search->location = $obj->loc;
		$search->country = $obj->country;
		$search->save();

		return  View::make('pages.result')->with('items', $items)->with('search',$se);
	}

	public function showSearch(){
		$date = date("Y-m-d H:i:s");
		$items = Item::whereRaw('DATE_ADD(created_at, INTERVAL 24 HOUR) > NOW()')
			->paginate(10);

		return  View::make('pages.home2')->with('items', $items);
	}

	public function saveItem(){

		$access = Input::get('api'); //Access Code [Y2FkZWF0aA==]
		$charname = Input::get('ch');
		$item = Input::get('itm');
		$price = Input::get('c');
		$location = Input::get('loc');
		$itemcode = Eskie::RandomString(5);

		if($access != 'Y2FkZWF0aA==') return 0;

		while(true){
			$code = Item::where('itemcode', $itemcode)->get();
			if(count($code) == 0) {
				break;
			}				
			$itemcode = Eskie::RandomString(5);
		}

		Item::create(array(
			'charname'	=>	$charname,
			'item'		=>	$item,
			'price'		=>	$price,
			'location'	=>	$location,
			'itemcode'	=>	$itemcode
		));

		echo "$itemcode";
	}

	public function sold(){

		$access = Input::get('api'); //Access Code [Y2FkZWF0aA==]
		$itemcode = Input::get('code');

		if($access != 'Y2FkZWF0aA==')
			return 0;

		$item = Item::where('itemcode', $itemcode)
			->where('isSold', 1)
			->get();
		if(count($item) == 1)
			return $item[0]->charname;

		Item::where('itemcode','=', $itemcode)
			->where('isSold','=',0)
			->update(array('isSold'=>1));

		$item = Item::where('itemcode', $itemcode)
			->where('isSold', 1)
			->get();
		if(count($item)==0)
			return "Item Not Found";

		return $item[0]->charname;
	}
}